package com.nlabs.slickgwt.client;

import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Widget;

public class BorderLayoutPanel extends AbsolutePanel implements ResizeHandler {

		private Widget m_north_widget = null;
		private Widget m_south_widget = null;
		private Widget m_east_widget = null;
		private Widget m_west_widget = null;
		private Widget m_center_widget = null; 
		private int m_margin_left = 5;
		private int m_margin_top = 5;
		private int m_margin_bottom = 5;
		private int m_margin_right = 5; 
	
		public BorderLayoutPanel() {
			
			super();
			setSize("100%", "100%");
			com.google.gwt.user.client.Window.addResizeHandler(this);
		}
		
		public void addNorthWidget(Widget w, int height){
			
			m_north_widget = w; 
			m_north_widget.setHeight("" + height);
			
		}
		
		public void addSouthWidget(Widget w, int height){
			
			m_south_widget = w;
			m_south_widget.setHeight("" + height);
		}
		
		public void addEastWidget(Widget w, int width){
			
			m_east_widget = w;
			m_east_widget.setWidth("" + width);
			
			
		}
		
		public void addWestWidget(Widget w, int width){
		
			m_west_widget = w;
			m_west_widget.setWidth("" + width);
		
		}
		
		public void addCenterWidget(Widget w){
			
			m_center_widget = w;
			
		}
		
		
		public void layout() {
			
			int ypos = m_margin_top;
			int xpos = m_margin_left; 
			
			if(m_north_widget != null){
				
				add(m_north_widget);
				this.setWidgetPosition(m_north_widget,xpos,ypos);
				int width = getOffsetWidth() - m_margin_right - xpos; 
				if(width > 0){
					m_north_widget.setWidth("" + width);
				}
				ypos = ypos + m_north_widget.getOffsetHeight(); 
				
			}
			
			int middle_stop = getOffsetHeight() - m_margin_bottom; 
 
			if(m_south_widget != null){
				
				add(m_south_widget);
				middle_stop = middle_stop  - m_south_widget.getOffsetHeight();
				this.setWidgetPosition(m_south_widget, xpos, middle_stop); 
				int width = getOffsetWidth() - m_margin_right - xpos; 
				if(width > 0){
					
					m_south_widget.setWidth("" + width);
				}
				
			}
			
			if(m_west_widget != null) {
				
				add(m_west_widget);
				setWidgetPosition(m_west_widget,xpos, ypos);
				xpos = xpos + m_west_widget.getOffsetWidth();
				int height = middle_stop - ypos ;
				if(height > 0){
					
					m_west_widget.setHeight("" + height);
				}
				
			}
			
			int center_stop = getOffsetWidth() - m_margin_right; 
			
			if(m_east_widget != null) {
				
				add(m_east_widget);
				center_stop = center_stop - m_east_widget.getOffsetWidth();
				setWidgetPosition(m_east_widget, center_stop, ypos);
				int height = middle_stop - ypos ;
				if(height > 0){
					
					m_east_widget.setHeight("" + height);
				} 
				
			}
			 
			if(m_center_widget != null){
				
				add(m_center_widget);
				setWidgetPosition(m_center_widget, xpos, ypos);
				int width = center_stop - xpos;
				if(width > 0){
					m_center_widget.setWidth("" + width);
				}
				int height = middle_stop - ypos ;
				if(height > 0){
					
					m_center_widget.setHeight("" + height);
				}
				 
				
			}
			
		}
		
		
		public void onAttach() {
		 
			layout(); 
			super.onAttach();
		}
		
		public void setPixelSize(int width, int height){
			
			super.setPixelSize(width, height);
			if(isAttached()){
				layout(); 
			}
		}
		
		public void setSize(String width, String height){
			 
			super.setSize(width, height);
			if(isAttached()){
				layout();
			}
		}

		public void onResize(ResizeEvent event) {
			if(isAttached()){ 
				layout();
			}
			
		}
 
		
		
		
}
